﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLibrary;

namespace BusinessLayer
{
    public class ClsCompanyInfo
    {
        public int CompanyInfoID { get; set; }
        public string CompanyName { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
        public string MobileNumber { get; set; }
        public string TelephoneNumber { get; set; }
        public string FaxNumber { get; set; }
        public string Email { get; set; }





        public DataTable GetAllCompanyInfo()
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select * from CompanyInfo", "CompanyInfo");

                if (dt.Rows.Count > 0)
                {
                    dt.Columns.Add("CompanyDESCR");
                    string CompanyDESCR = dt.Rows[0]["Address1"].ToString()
                                        + " " + dt.Rows[0]["Addresss2"].ToString()
                                        + " " + dt.Rows[0]["City"].ToString()
                                        + " " + dt.Rows[0]["State"].ToString()
                                        + " " + dt.Rows[0]["ZipCode"].ToString()
                                         + ", Mobile: " + dt.Rows[0]["MobileNumber"].ToString()
                                         + ", Telephone: " + dt.Rows[0]["TelephoneNumber"].ToString()
                                         + ", Fax: " + dt.Rows[0]["FaxNumber"].ToString()
                                         + ", Email: " + dt.Rows[0]["Email"].ToString().ToLower();

                    dt.Rows[0]["CompanyDESCR"] = CompanyDESCR;
                }
                return dt;
            }
        }
        public void GetCompanyInfo(int intCompanyInfoID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select * from CompanyInfo where CompanyInfoID=" + intCompanyInfoID, "CompanyInfo");
                if (dt.Rows.Count > 0)
                {
                    CompanyInfoID = Convert.ToInt32(dt.Rows[0]["CompanyInfoID"]);
                    CompanyName = dt.Rows[0]["CompanyName"].ToString();
                    Address1 = dt.Rows[0]["Address1"].ToString();
                    Address2 = dt.Rows[0]["Addresss2"].ToString();
                    City = dt.Rows[0]["City"].ToString();
                    State = dt.Rows[0]["State"].ToString();
                    ZipCode = dt.Rows[0]["ZipCode"].ToString();
                    MobileNumber = dt.Rows[0]["MobileNumber"].ToString();
                    TelephoneNumber = dt.Rows[0]["TelephoneNumber"].ToString();
                    FaxNumber = dt.Rows[0]["FaxNumber"].ToString();
                    Email = dt.Rows[0]["Email"].ToString();

                }
            }
        }
        public bool SaveCompanyInfo(string KeyForSave)
        {

            using (OleDataClient data = new OleDataClient())
            {
                if (KeyForSave == "add")
                {

                    string count = data.GetValue("select count(*) from CompanyInfo where CompanyName='" + CompanyName + "'");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("CompanyName already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Insert into CompanyInfo (CompanyName,Address1,Addresss2,City,State,ZipCode,MobileNumber,TelephoneNumber,FaxNumber,Email) values('" + CompanyName + "','" + Address1 + "','" + Address2 + "','" + City + "','" + State + "','" + ZipCode + "','" + MobileNumber + "','" + TelephoneNumber + "','" + FaxNumber + "','" + Email + "'" + " )");
                    }

                }
                else
                {

                    string count = data.GetValue("select count(*) from CompanyInfo where CompanyName ='" + CompanyName + "' and CompanyInfoID NOT IN (" + CompanyInfoID.ToString() + ")");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("CompanyName already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Update CompanyInfo set CompanyName = '" + CompanyName + "', Address1 =  '" + Address1 + "', Addresss2 =  '" + Address2 + "', City =  '" + City + "', State =  '" + State + "', ZipCode =  '" + ZipCode + "', MobileNumber =  '" + MobileNumber + "',TelephoneNumber =  '" + TelephoneNumber + "',FaxNumber =  '" + FaxNumber + "', Email =  '" + Email + "' where CompanyInfoID=" + CompanyInfoID.ToString());
                    }


                }
            }
            return true;
        }
        public void DeleteCompanyInfo()
        {
            using (OleDataClient data = new OleDataClient())
            {
                data.ExecuteNonQuery("Delete from  CompanyInfo where CompanyInfoID=" + CompanyInfoID.ToString());
            }
        }

    }
}
